# 视图
import numpy as np

a = np.arange(10)
print(a)
a1 = a[2:7]
print(a1)
a1[2] = 100
print(a1) 
print(a) 

# 拷贝
a2 = a[2:7].copy()
a2[2] = 200
print(a2)
print(a)
print("---------------------")
# 数学运算
a = np.array([1, 2, 3, 4])
b = np.array([5, 6, 7, 8])
print(a + b)
print(a - b)
print(a * b)
print("---------------------")
# 矩阵运算
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([[7, 8, 9], [10, 11, 12]])
print(a + b)
print(a - b)
print(a * b)
print(a / b)

# 数学开方
a = np.array([1, 2, 3, 4])
print(np.sqrt(a))
print(np.sqrt(a)*np.sqrt(a))

# 指数运算
print(np.exp(a))   # 指数
print(np.sin(a))   # 正弦
print(np.log(a))   # 自然对数


#  统计函数
# 均值:算术平均值
a = np.array([1, 2, 3, 4])
print(np.mean(a)) # 2.5

# 多维数组的mean
a = np.array([[1, 2, 3],
              [4, 5, 6]])
print(np.mean(a)) # 3.5
print(np.mean(a, axis=0)) # [2.5 3.5 4.5]
print(np.mean(a, axis=1)) # [2. 5.]

# 中位数
a = np.array([1,2,3,4])
print(np.median(a)) # 2.5
a = np.array([1,2,4,7,8,])
print(np.median(a)) # 5.5

# 最大值和最小值
a = np.array([1,2,3,4])
print(np.max(a)) # 4
print(np.min(a)) # 1

# 多维数组的最大值和最小值
a = np.array([[1,2,3],
              [4,5,6]])
print(np.max(a)) # 6
print(np.min(a)) # 1
print(np.max(a, axis=0)) # [4 5 6]
print(np.max(a, axis=1)) # [3 6]


# 方差：离散变量的离散程度
a = np.array([1,2,3,4])
print(np.var(a)) # 1.25

# 标准差：离散变量的离散程度，与方差不同的是，标准差是方差的平方根
a = np.array([1,2,3,4])
print(np.std(a)) # 1.118033988749895

# 协方差：衡量两个变量之间的线性关系
a = np.array([1,2,3,4])
b = np.array([5,6,7,8])
print(np.cov(a, b)) # 1.25
print(np.cov(a, b, bias=True)) # 1.0

# 百分位数
a = np.array([1,2,3,4])
print(np.percentile(a, 50)) # 2.5


print("---------------------")
# 加权平均值
a = np.array([1,2,3,4])
# 权重数组
w = np.array([0.1, 0.2, 0.3, 0.4])
print(np.average(a, weights=w)) # 2.6


